package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.IdIntegral;
import com.itheima.pojo.KsClass;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findByClassId(Integer classId) {
        List<User> users = userMapper.findByClassId(classId);
        for (User user : users) {
            Integer groupId = user.getGroupId();
            Integer groupsIntegral = 0;
            for (User user1 : users) {
                if (user1.getGroupId() == groupId){
                    groupsIntegral += user1.getIntegral();
                }
            }
            for (User user1 : users) {
                if (user1.getGroupId() == groupId){
                     user1.setGroupsIntegral(groupsIntegral);
                }
            }
            user.setGroupsIntegral(groupsIntegral);
        }
        return users;
    }

    @Override
    public boolean updateById(IdIntegral idIntegral) {
        Integer integral = userMapper.findById(idIntegral)+ idIntegral.getIntegral();
        idIntegral.setIntegral(integral);
        int i = userMapper.updateById(idIntegral);
        return i>0;
    }

    @Override
    public ResponseResult updateIntegralByClassId(Integer classId) {
        int i = userMapper.updateIntegralByClassId(classId);
        if (i>0){
            return new ResponseResult(0,"操作成功",null);
        }
        return new ResponseResult(1,"操作失败",null);
    }

}